﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Dashboard.ClusteringSvc;
using Dashboard.SettingSvc;
using Liquid;

namespace Dashboard
{
    public partial class MyDataTree : UserControl
    {
        public event OnClusterResultSelected ClusterResultSelected;

        private const string ClusterResultConnNameKey = "ClusterResultConnName";
        private const string ClusterResultTableNameKey = "ClusterResultTableName";
        private const string ClusterResultNameFieldKey = "ClusterResultNameField";
        private const string ClusterResultOutputFieldKey = "ClusterResultOutputField";
        private Dictionary<string, string> settings;

        public MyDataTree()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MyDataTree_Loaded);
        }

        void MyDataTree_Loaded(object sender, RoutedEventArgs e)
        {
            this.treeData.Nodes.Clear();
            Node clusteringRootTN = new Node("Clusters", "Clusters", false);
            this.treeData.Nodes.Add(clusteringRootTN);
            Node reportRootTN = new Node("Reports", "Reports", false);
            this.treeData.Nodes.Add(reportRootTN);
            Node networkRootTN = new Node("Networks", "Networks", false);
            this.treeData.Nodes.Add(networkRootTN);

            ObservableCollection<string> keys = new ObservableCollection<string>();
            keys.Add(ClusterResultConnNameKey);
            keys.Add(ClusterResultTableNameKey);
            keys.Add(ClusterResultNameFieldKey);
            keys.Add(ClusterResultOutputFieldKey);
            ServerSettingsClient client = new ServerSettingsClient();
            client.GetSettingsCompleted += new EventHandler<GetSettingsCompletedEventArgs>(client_GetSettingsCompleted);
            client.GetSettingsAsync(keys);
        }

        void client_GetSettingsCompleted(object sender, GetSettingsCompletedEventArgs e)
        {
            this.settings = e.Result;
            ClusteringServiceClient client = new ClusteringServiceClient();
            string connName = this.settings[ClusterResultConnNameKey];
            string tableName = this.settings[ClusterResultTableNameKey];
            string taskFieldName = this.settings[ClusterResultNameFieldKey];
            client.GetClusteringResultNamesCompleted += new EventHandler<GetClusteringResultNamesCompletedEventArgs>(client_GetClusteringResultNamesCompleted);
            client.GetClusteringResultNamesAsync(connName, tableName, taskFieldName);
        }

        void client_GetClusteringResultNamesCompleted(object sender, GetClusteringResultNamesCompletedEventArgs e)
        {
            Node clusteringRootTn = null;
            foreach (Node node in this.treeData.Nodes)
            {
                if (node.Title == "Clusters")
                {
                    clusteringRootTn = node;
                    break;
                }
            }
            if (clusteringRootTn != null && e.Result != null)
            {
                clusteringRootTn.HasChildren = true;
                foreach (string resultName in e.Result)
                {
                    Node clusterTn = new Node(resultName, resultName, false);
                    clusteringRootTn.Nodes.Add(clusterTn);
                    clusterTn.MouseLeftButtonDown += new MouseButtonEventHandler(clusterTn_MouseLeftButtonDown);
                }
                clusteringRootTn.IsExpanded = true;
                clusteringRootTn.ExpandAll();
            }
        }

        void clusterTn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Node clusterTN = sender as Node;
            if(clusterTN !=null && !string.IsNullOrEmpty(clusterTN.Title))
            {
                string resultName = clusterTN.Title;
                string connName = this.settings[ClusterResultConnNameKey];
                string tableName = this.settings[ClusterResultTableNameKey];
                string taskFieldName = this.settings[ClusterResultNameFieldKey];
                string dataFieldName = this.settings[ClusterResultOutputFieldKey];
                ClusteringServiceClient client = new ClusteringServiceClient();
                client.GetClusteringOutputCompleted += new EventHandler<GetClusteringOutputCompletedEventArgs>(client_GetClusteringOutputCompleted);
                client.GetClusteringOutputAsync(
                    connName, tableName, taskFieldName, 
                    dataFieldName, resultName);
            }
        }

        void client_GetClusteringOutputCompleted(object sender, GetClusteringOutputCompletedEventArgs e)
        {
            if (this.ClusterResultSelected != null)
                this.ClusterResultSelected(e.Result);
        }
    }
}
